<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<%_ const reactivePrefix = reactive ? 'Reactive' : ''; _%>
package <%= packageName %>.web.rest;

import static <%= packageName %>.test.util.OAuth2TestUtil.ID_TOKEN;
import static <%= packageName %>.test.util.OAuth2TestUtil.authenticationToken;
import static <%= packageName %>.test.util.OAuth2TestUtil.registerAuthenticationToken;
<%_ if (reactive) { _%>
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.*;
<%_ } else { _%>
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
<%_ } _%>

import <%= packageName %>.IntegrationTest;
import <%= packageName %>.security.AuthoritiesConstants;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.oauth2.client.<% if (reactive) { %>Reactive<% } %>OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.<% if (reactive) { %>Reactive<% } %>ClientRegistrationRepository;
<%_ if (reactive) { _%>
import org.springframework.context.ApplicationContext;
import org.springframework.test.web.reactive.server.WebTestClient;
<%_ } else { _%>
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
<%_ } _%>

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Integration tests for the {@link LogoutResource} REST controller.
 */
@IntegrationTest
class LogoutResourceIT {

    @Autowired
    private <% if (reactive) { %>Reactive<% } %>ClientRegistrationRepository registrations;

    @Autowired
    private <% if (!reactive) { %>Web<% } %>ApplicationContext context;

    @Autowired
    private <%= reactivePrefix %>OAuth2AuthorizedClientService authorizedClientService;

    @Autowired
    private ClientRegistration clientRegistration;

<%_ if (reactive) { _%>
    private WebTestClient webTestClient;
<%_ } else { _%>
    private MockMvc restLogoutMockMvc;
<%_ } _%>

    private Map<String, Object> claims;

    @BeforeEach
    void before() <% if (!reactive) { %>throws Exception <% } %>{
        claims = new HashMap<>();
        claims.put("groups", Collections.singletonList(AuthoritiesConstants.USER));
        claims.put("sub", 123);

<%_ if (reactive) { _%>
        this.webTestClient = WebTestClient.bindToApplicationContext(this.context)
            .apply(springSecurity())
            .configureClient()
            .build();
<%_ } else { _%>
        SecurityContextHolder
            .getContext()
            .setAuthentication(registerAuthenticationToken(authorizedClientService, clientRegistration, authenticationToken(claims)));
        SecurityContextHolderAwareRequestFilter authInjector = new SecurityContextHolderAwareRequestFilter();
        authInjector.afterPropertiesSet();

        this.restLogoutMockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
<%_ } _%>
    }

    @Test
    void getLogoutInformation() <% if (!reactive) { %>throws Exception <% } %>{
        final String ORIGIN_URL = "http://localhost:8080";
<%_ if (reactive) { _%>
        String logoutUrl = this.registrations.findByRegistrationId("oidc")
            .map(oidc -> oidc.getProviderDetails().getConfigurationMetadata()
                .get("end_session_endpoint").toString()).block();
        logoutUrl = logoutUrl + "?id_token_hint=" + ID_TOKEN + "&post_logout_redirect_uri=" + ORIGIN_URL;
        this.webTestClient.mutateWith(csrf())
            .mutateWith(mockAuthentication(registerAuthenticationToken(authorizedClientService, clientRegistration, authenticationToken(claims))))
            .post().uri("http://localhost:8080/api/logout").header(HttpHeaders.ORIGIN, ORIGIN_URL).exchange()
            .expectStatus().isOk()
            .expectHeader().contentType(MediaType.APPLICATION_JSON_VALUE)
            .expectBody()
            .jsonPath("$.logoutUrl").isEqualTo(logoutUrl);
<%_ } else { _%>
        String logoutUrl = this.registrations.findByRegistrationId("oidc").getProviderDetails()
            .getConfigurationMetadata().get("end_session_endpoint").toString();
        logoutUrl = logoutUrl + "?id_token_hint=" + ID_TOKEN + "&post_logout_redirect_uri=" + ORIGIN_URL;
        restLogoutMockMvc.perform(post("http://localhost:8080/api/logout").header(HttpHeaders.ORIGIN, ORIGIN_URL))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.logoutUrl").value(logoutUrl));
<%_ } _%>
    }
}
